import {PageContainer} from "@ant-design/pro-components";
import DictDataTable from "@/pages/DictData/components/DictDataTable";
import {DictDataVO, saveDictData, updateDictData} from "@/services/DictDataService";
import DictDataUpdateModal from "@/pages/DictData/components/DictDataUpdateModal";
import {useState} from "react";
import {message} from "antd";

const DictData = () => {
    // 编辑操作
    const [updateModalState, setUpdateModalState] = useState(false)
    const [currentDictData, setCurrentDictData] = useState<DictDataVO | undefined>()
    const updateEvent = async (record: DictDataVO) => {
        // 打开模态框
        setCurrentDictData(record)
        setUpdateModalState(true)
    }

    const saveEvent = async () => {
        // 打开模态框
        setUpdateModalState(true)
    }

    const onUpdateModalChange = async (state: boolean) => {
        if (state) {
            setUpdateModalState(state);
        } else {
            await setCurrentDictData(undefined)
            setUpdateModalState(state)
        }
    }
    // 提交修改
    const onUpdateFinish = async (formData: any) => {
        let response
        if (currentDictData) {
            response = await updateDictData(formData);
        } else {
            response = await saveDictData(formData)
        }
        if (response) {
            message.success("操作成功")
            return true
        }
        return false;
    }

    return <PageContainer title="字典数据">
        {/*字典数据表格 开始*/}
        <DictDataTable onSave={saveEvent} onUpdate={updateEvent}/>
        {/*字典数据表格 结束*/}

        {/*修改模态框 开始*/}
        <DictDataUpdateModal
                dictData={currentDictData}
                open={updateModalState}
                onOpenChange={onUpdateModalChange}
                onFinish={onUpdateFinish}/>
        {/*修改模态框 结束*/}
    </PageContainer>
}

export default DictData
